package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.Category;
import com.ruoyi.business.domain.Depart;
import com.ruoyi.business.service.ICategoryService;
import com.ruoyi.business.service.IDepartService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Content;
import com.ruoyi.business.service.IContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * ContentController
 *
 * @author win
 * @date 2021-06-14
 */
@RestController
@RequestMapping("/Content/content")
public class ContentController extends BaseController
{
    @Autowired
    private IContentService contentService;

    @Autowired
    private ICategoryService categoryService;

    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private IDepartService departService;

    /**
     * 查询Content列表
     */
//    @PreAuthorize("@ss.hasPermi('business:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(Content content)
    {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("business_belong");
        List<SysDictData> belongList = dictDataService.selectDictDataList(sysDictData);

        startPage();
        List<Content> list = contentService.selectContentList(content);
        list.forEach(item -> {
            Category parent = categoryService.selectCategoryById(item.getCategoryId());
            if (parent != null) {
                item.setCategoryName(parent.getTitle());
            }
            for (SysDictData dictData : belongList) {
                if (item.getBelong().toString().equals(dictData.getDictValue())) {
                    item.setBelongName(dictData.getDictLabel());
                }
            }
            Depart depart = departService.selectDepartById(item.getDepartId());
            item.setDepartName(depart != null ? depart.getName() : "");
        });
        return getDataTable(list);
    }

    @GetMapping("/auditList")
    public TableDataInfo auditList(Content content)
    {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("business_belong");
        List<SysDictData> belongList = dictDataService.selectDictDataList(sysDictData);

        startPage();
        List<Content> list = contentService.selectAuditContentList(content);
        list.forEach(item -> {
            Category parent = categoryService.selectCategoryById(item.getCategoryId());
            if (parent != null) {
                item.setCategoryName(parent.getTitle());
            }
            for (SysDictData dictData : belongList) {
                if (item.getBelong().toString().equals(dictData.getDictValue())) {
                    item.setBelongName(dictData.getDictLabel());
                }
            }
            Depart depart = departService.selectDepartById(item.getDepartId());
            item.setDepartName(depart != null ? depart.getName() : "");
        });
        return getDataTable(list);
    }

    /**
     * 导出Content列表
     */
//    @PreAuthorize("@ss.hasPermi('business:content:export')")
    @Log(title = "Content", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Content content)
    {
        List<Content> list = contentService.selectContentList(content);
        ExcelUtil<Content> util = new ExcelUtil<Content>(Content.class);
        return util.exportExcel(list, "Content数据");
    }

    /**
     * 获取Content详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(contentService.selectContentById(id));
    }

    /**
     * 新增Content
     */
//    @PreAuthorize("@ss.hasPermi('business:content:add')")
    @Log(title = "Content", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Content content)
    {
        return toAjax(contentService.insertContent(content));
    }

    /**
     * 修改Content
     */
//    @PreAuthorize("@ss.hasPermi('business:content:edit')")
    @Log(title = "Content", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Content content)
    {
        return toAjax(contentService.updateContent(content));
    }

    /**
     * 删除Content
     */
//    @PreAuthorize("@ss.hasPermi('business:content:remove')")
    @Log(title = "Content", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(contentService.deleteContentByIds(ids));
    }
}
